﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class api_uploadImg : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ResponseData rd = isLogin();
        if (rd.code != 1)
        {
            Json(rd);
            return;
        }
        string action = Request["action"];
        switch (action)
        {
            case "uploadOne"://上传1个文件
                uploadOne();
                break;
            default:
                break;
        }
    }

    private void uploadOne()
    {
        ResponseData rd = new ResponseData();
        try
        {
            //Hashtable h_p = getPostParameter_json();
            //object gameName = h_p["gameName"];
            //if (gameName == null || gameName.ToString() == "")
            //{
            //    rd.code = 0;
            //    rd.message = "请传游戏名称";
            //    Json(rd);
            //    return;
            //}
            HttpFileCollection s = Request.Files;
            string rootPath = Pub.GetRootPath() + "api/file/gameList/"; //文件要保存的目录
            if (!Directory.Exists(Pub.GetRootPathAbsolute() + rootPath))
            {
                Directory.CreateDirectory(Pub.GetRootPathAbsolute() + rootPath);
            }
            // 保存文件地址
            string saveFileName = GetDateTimeStringShort() + ".png";
            //string saveFileName = gameName.ToString();
            //保存文件
            s[0].SaveAs(Pub.GetRootPathAbsolute(rootPath + saveFileName));
            rd.code = 1;
            rd.data = "/file/gameList/" + saveFileName;
            rd.message = "图片保存成功";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    public void uploadFun()
    {
        string msg = string.Empty;
        int code = 0;

        HttpFileCollection s = Request.Files;
        string directoryPath = "Upfile/Images/";
        string rootPath = Pub.GetRootPath() + "/" + directoryPath; //文件要保存的目录

        string TreeNode = Request["TreeNode"];
        //List<string> pathRes = new List<string>();
        int sum = 0; int fg = 0; string filenames = ""; string isfugai = "";
        if (s.Count > 0)
        {
            try
            {
                if (!Directory.Exists(Pub.GetRootPathAbsolute() + rootPath))
                {
                    Directory.CreateDirectory(Pub.GetRootPathAbsolute() + rootPath);
                }
                for (int i = 0; i < s.Count; i++)
                {
                    //文件名称+后缀
                    string fileName = s[i].FileName;
                    string imgSaveName = TreeNode + "_" + fileName;
                    //string houzhui = filename1.Substring(filename1.LastIndexOf('.'));

                    //上传数据库记录
                    //string fanhui = insertImg(fileName, base64Img, TreeNode);
                    //string fanhui = "";

                    Yun_Img img = new Yun_Img();
                    int iii = img.Getcunzai(imgSaveName);//检查是否存在
                    if (iii > 0)
                    {
                        filenames += fileName + ",";
                        isfugai = "1";
                    }

                    // 保存文件地址
                    string saveFileName = rootPath + imgSaveName;
                    //保存文件
                    s[i].SaveAs(Pub.GetRootPathAbsolute(saveFileName));

                    Hashtable h = new Hashtable();
                    h["imgClass"] = TreeNode;
                    h["imgName"] = fileName;
                    h["imgSuffix"] = fileName.Split('.')[1];
                    h["imgSaveName"] = TreeNode + "_" + fileName;

                    //图片转base64
                    string tmpRootDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString());
                    var base64Img = Convert.ToBase64String(File.ReadAllBytes(tmpRootDir + directoryPath + imgSaveName));
                    h["imgBase64"] = base64Img;
                    h["createdate"] = DateTime.Now.ToString();
                    if (isfugai == "1")
                    {
                        isfugai = "";
                        int ii = img.Update_imgName(h);
                        if (ii > 0)
                        {
                            fg++;
                        }
                        else
                        {
                            code = 0;
                            msg = "数据库记录失败";
                            Response.Write(ToJson(new
                            {
                                code = code,
                                msg = msg
                            }));
                            return;
                        }
                    }
                    else
                    {
                        int ii = img.insert(h);
                        if (ii > 0)
                        {
                            sum++;
                        }
                        else
                        {
                            code = 0;
                            msg = "数据库记录失败";
                            Response.Write(ToJson(new
                            {
                                code = code,
                                msg = msg
                            }));
                            return;
                        }
                    }



                    //pathRes.Add(string.Format("../{0}{1}", directoryPath, fileName));
                }

                string fgtishi = ""; string tishi = "";
                if (fg > 0)
                {
                    fgtishi = " 覆盖 " + fg + " 条";
                }
                if (sum > 0)
                {
                    tishi = "上传 " + sum + " 条.";
                }
                code = 1;
                msg = tishi + fgtishi;
            }
            catch (Exception ex)
            {
                code = 0;
                msg = ex.Message;
            }
        }
        else
        {
            msg = "没找到对应的文件！";
        }
        Response.Write(ToJson(new
        {
            code = code,
            msg = msg
        }));
    }

}